package com.dpad.telematicsclientapp.adapter

import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.TextAppearanceSpan
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

import com.dpad.crmclientapp.android.util.utils.itemBottom
import com.dpad.crmclientapp.android.util.utils.itemMiddle
import com.dpad.crmclientapp.android.util.utils.itemTop
import com.dpad.telematicsclientapp.android.R
import com.dpad.telematicsclientapp.mvp.kit.Kits
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.UserCenterBean
import com.dpad.telematicsclientapp.util.utils.DateUtils

/**
 * ================================================
 * 作    者：wenbody
 * 版    本：1.0
 * 创建日期：2019/12/19 16:56
 * 修订历史：
 * ================================================
 */
class UserCenterAdapter(context: Context, data: MutableList<UserCenterBean>) : BaseMultiItemQuickAdapter<UserCenterBean, BaseViewHolder>(data) {
    private var myContext = context

    init {
        addItemType(itemTop, R.layout.item_user_center_top)
        addItemType(itemMiddle, R.layout.item_user_center_middle)
        addItemType(itemBottom, R.layout.item_usercenter_bottom)
    }

    override fun convert(helper: BaseViewHolder, item: UserCenterBean) {
        when (item.eventType) {
            itemTop -> {
                helper.setText(R.id.itemDate, item.data.eventTime.replace("-", "."))
                helper.setText(R.id.itemWeek, DateUtils.dateToWeek(item.data.eventTime))
            }
            itemMiddle -> {
                helper.setText(R.id.userCenterDetailTv, item.data.eventName)
                helper.setText(R.id.userCenterDetailWithBgTv, item.data.eventName)
                if(needSetjump(item.data.eventType)){
                    helper.setVisible(R.id.userCenterDetailIm,false)
                }else{
                    helper.setVisible(R.id.userCenterDetailIm,true)
                }
                if (needSetBg(item.data.eventType)) {
                    helper.getView<TextView>(R.id.userCenterDetailTv).visibility = View.GONE
                    helper.getView<LinearLayout>(R.id.userBgLl).visibility = View.VISIBLE
                    helper.setText(R.id.userCenterDetailWithBgTv, changeTextViewColor(item.data.eventName))
                    if (item.data.eventName.contains("喜提爱车")) {
                        helper.getView<ImageView>(R.id.userCenterXdl).visibility = View.VISIBLE
                    } else {
                        helper.getView<ImageView>(R.id.userCenterXdl).visibility = View.GONE
                    }
                } else {
                    helper.getView<LinearLayout>(R.id.userBgLl).visibility = View.GONE
                    helper.getView<TextView>(R.id.userCenterDetailTv).visibility = View.VISIBLE
                }

            }
            itemBottom -> {
                helper.setText(R.id.userCenterDetailTv, item.data.eventName)
                helper.setText(R.id.userCenterDetailWithBgTv, item.data.eventName)
                if(needSetjump(item.data.eventType)){
                    helper.setVisible(R.id.userCenterDetailIm,false)
                }else{
                    helper.setVisible(R.id.userCenterDetailIm,true)
                }
                if (needSetBg(item.data.eventType)) {
                    helper.getView<TextView>(R.id.userCenterDetailTv).visibility = View.GONE
                    helper.getView<LinearLayout>(R.id.userBgLl).visibility = View.VISIBLE
                    helper.setText(R.id.userCenterDetailWithBgTv, changeTextViewColor(item.data.eventName))
                    if (item.data.eventName.contains("喜提爱车")) {
                        helper.getView<ImageView>(R.id.userCenterXdl).visibility = View.VISIBLE
                    } else {
                        helper.getView<ImageView>(R.id.userCenterXdl).visibility = View.GONE
                    }
                } else {
                    helper.getView<LinearLayout>(R.id.userBgLl).visibility = View.GONE
                    helper.getView<TextView>(R.id.userCenterDetailTv).visibility = View.VISIBLE
                }
            }
        }
    }

    /**
     * 是否需要设置背景
     */
    private fun needSetBg(eventType: String): Boolean {
        return !Kits.Empty.check(eventType) && (
                eventType == "towner_buy_car_time"
//                        || eventType == "tourists_happy_car"
//                        || eventType == "owner_first_login"
                        || eventType == "tourists_happy_car"
//                        || eventType == "tourists_register_time"
//                        || eventType == "tourists_first_login"
                        || eventType == "owner_buy_car_time"
//
                        || eventType == "owner_register_time"
                        || eventType == "owner_happy_car"
                        || eventType == "owner_register_time"


//                        || eventType == "tourists_test_drive"
                )
    }
    private fun needSetjump(eventType: String): Boolean {
        return !Kits.Empty.check(eventType) && (
                eventType == "owner_first_login"
                        || eventType == "tourists_first_login"
                        || eventType == "tourists_happy_car"
                        || eventType == "tourists_register_time"
                        || eventType == "towner_buy_car_time"
                        || eventType == "owner_buy_car_time"
                        || eventType == "owner_happy_car"
                        || eventType == "owner_register_time"
                        || eventType == "maintenance_records"
                )
    }
    /**
     * 商城订单	mall_order
    当前车辆的保养记录	maintenance_records
    取消救援	rescues_update
    发起救援	rescues_add
    发起预约单	appointment_add
    取消预约单	appointment_update
    预约试驾	tourists_test_drive
    首次登录	owner_first_login
    首次登录	tourists_first_login


    成为车主	tourists_happy_car(游客 喜提爱车)
    注册纪念日	tourists_register_time(游客)
    买车纪念日	owner_buy_car_time
    成为车主	owner_happy_car(喜提爱车)
    注册纪念日	owner_register_time
     */

    private fun changeTextViewColor(string: String): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder(string)
        for (index in spannableStringBuilder.indices) {
            if (Character.isDigit(string[index])) {
                spannableStringBuilder.setSpan(TextAppearanceSpan(myContext, R.style.text_number_primary), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            } else {
                spannableStringBuilder.setSpan(TextAppearanceSpan(myContext, R.style.text_number_white), index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
        }
        return spannableStringBuilder
    }
}